const express = require('express')
const router = express.Router()
const fs = require('fs')
const path = require("path")
const formidable = require("formidable")
const mysql = require('mysql')

const key = require("../lib/state")
const getToken = require('../components/getToken')

const wordRecognition = require('../components/word_recognition')
const animalRecognition = require("../components/animal_recognition")
const dishRecognition = require("../components/dish_recognition")
const logoRecognition = require("../components/logo_recognition")
const general = require("../components/general_recognition")
const vegetableRecogition = require("../components/vegetable_recognition")
const currencyRecognition = require("../components/currency_recognition")
const plantRecognition = require("../components/plant_recognition")
const landmarkRecognition = require("../components/landmark_recognition")

const getNewName = require("../components/getNewName")

let db = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'byy'
});

getToken(key.image.apiKey, key.image.secretKey, "image")
getToken(key.word.apiKey, key.word.secretKey, "word")

let word_tokenList = fs.readFileSync('./word-token.json', 'utf8')
let image_tokenList = fs.readFileSync('./image-token.json', 'utf8')
let word_token = JSON.parse(word_tokenList).access_token
let image_token = JSON.parse(image_tokenList).access_token

router.use((req, res, next) => {
    console.log('进入recognition', Date.now())
    next()
})

router.post('/wordRecognition', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {
        // console.log(files); //文件
        // console.log(fields);//其他formData
        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`,(err,data)=>{
            console.log(err)
            console.log(data)
        })
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        wordRecognition(word_token, "./page/upload/" + newName, res)
    })
})

router.post('/animalRecognition', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {

        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`)
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        animalRecognition(image_token, "./page/upload/" + newName, res)
    })
})


router.post('/dishRecognition', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {
        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`)
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        dishRecognition(image_token, "./page/upload/" + newName, res)
    })
})

router.post('/logoRecognition', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {
        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`)
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        logoRecognition(image_token, "./page/upload/" + newName, res)
    })
})

router.post('/general', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {

        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`)
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        general(image_token, "./page/upload/" + newName, res)
    })
})

router.post('/vegetableRecogition', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {

        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`)
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        vegetableRecogition(image_token, "./page/upload/" + newName, res)
    })
})
router.post('/landmarkRecognition', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {

        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`)
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        landmarkRecognition(image_token, "./page/upload/" + newName, res)
    })
})
router.post('/currencyRecognition', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {

        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`)
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        currencyRecognition(image_token, "./page/upload/" + newName, res)
    })
})

router.post('/plantRecognition', (req, res) => {
    var form = new formidable.IncomingForm();
    form.encoding = 'utf-8';
    form.uploadDir = path.join(__dirname + "/../page/upload");
    form.keepExtensions = true;//保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024;
    //处理图片
    form.parse(req, (err, fields, files) => {

        let newName = getNewName(files,fields)
        db.query(`insert into file (username,type,fileName) value('${fields.username}','${fields.type}','${newName}')`)
        let newPath = form.uploadDir + "/" + newName;
        fs.renameSync(files.file.path, newPath);  //重命名
        plantRecognition(image_token, "./page/upload/" + newName, res)
    })
})
module.exports = router